package com.ylsf.blog.service.impl;

import com.ylsf.blog.mapper.ArticleMapper;
import com.ylsf.blog.mapper.CommentMapper;
import com.ylsf.blog.service.DeleteService;
import com.ylsf.blog.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

/**
 * 删除服务实现类
 */
public class DeleteServiceImpl implements DeleteService {
    @Override
    public boolean deleteArticle(String articleId) {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        ArticleMapper articleMapper = sqlSession.getMapper(ArticleMapper.class);
        CommentMapper commentMapper = sqlSession.getMapper(CommentMapper.class);
        boolean flag=false;
        try {
            Integer integer1 = articleMapper.deleteArticle(articleId);
            Integer integer2 = commentMapper.deleteComment(articleId);
            if(integer1+integer2!=0){
                flag=true;
            }
            sqlSession.commit();
        }catch (Exception e) {
            if (sqlSession != null) {
                sqlSession.rollback(); // 在catch块中进行回滚操作
            }
        }finally {
            if (sqlSession != null) {
                SqlSessionUtil.close(sqlSession); // 在finally块中关闭SqlSession
            }
        }
        return flag;
    }
}
